#!/usr/bin/env python
#coding=utf-8
import os
import wx
import wx.stc

class wxApp(wx.App):
    def OnInit(self):
        return True

class Frame(wx.Frame):
    def __init__(self):
        super(Frame, self).__init__(None, -1)
        self.init()
        
    def init(self):
        menuBar = wx.MenuBar()
        menu = wx.Menu()
        menu.Append(wx.ID_OPEN, "&Open", "Open file.")
        menu.AppendSeparator()
        menu.Append(wx.ID_CLOSE, "E&xit", "Exit the program")
        menuBar.Append(menu, "&File")
        self.SetMenuBar(menuBar)
        
        self.editor = wx.stc.StyledTextCtrl(self)
        
        self.Bind(wx.EVT_MENU, self.OnOpen, id=wx.ID_OPEN)
        
    def OnOpen(self, event):
        dlg = wx.FileDialog(self, "Open a file", style=wx.OPEN)
        filename = ''
        if dlg.ShowModal() == wx.ID_OK:
            filename = dlg.GetPath()
        dlg.Destroy()
        
        if filename:
       	    self.editor.SetText(file(filename).read())
            ext = os.path.splitext(filename)[1]
            if ext == '.py':
                from py_color import py_color
                py_color(self.editor)
            elif ext == '.sql':
                from sql_color import sql_color
                self.Bind(wx.stc.EVT_STC_SYTLENEEDED, self.OnStyleNeeded, \
                        self.editor)
                sql_color(self.editor)
    def OnStyleNeeded(self, event):
        from sql_color import sql_do_color
        sql_do_color(self.editor, event.GetPosition())
        
if __name__ == '__main__':
    wxApp = wxApp(0)
    frame = Frame()
    frame.Show()
    wxApp.SetTopWindow(frame)
    wxApp.MainLoop()
